package com.lhj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhj.reggie.common.CustomException;
import com.lhj.reggie.common.R;
import com.lhj.reggie.dto.DishDto;
import com.lhj.reggie.dto.SetmealDto;
import com.lhj.reggie.entity.Category;
import com.lhj.reggie.entity.Dish;
import com.lhj.reggie.entity.Setmeal;
import com.lhj.reggie.entity.SetmealDish;
import com.lhj.reggie.mapper.CategoryMapper;
import com.lhj.reggie.mapper.DishMapper;
import com.lhj.reggie.mapper.SetmealDishMapper;
import com.lhj.reggie.mapper.SetmealMapper;
import com.lhj.reggie.service.DishService;
import com.lhj.reggie.service.SetmealDishService;
import com.lhj.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Resource
    private SetmealService setmealService;

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SetmealDishService setmealDishService;

    @Resource
    private SetmealDishMapper setmealDishMapper;

    public R<String> save( SetmealDto setmealDto){
        //保存套餐信息
        setmealMapper.insert(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
           item.setSetmealId(setmealDto.getId());
           return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联消息
        setmealDishService.saveBatch(setmealDishes);
        return R.success("新增套餐成功");
    }

    public R<Page> page(int page, int pageSize, String name){
        //创建分页构造器
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,Setmeal::getName,name);
        lqw.orderByAsc(Setmeal::getUpdateTime);
        setmealMapper.selectPage(page1,lqw);

        //对象拷贝
        BeanUtils.copyProperties(page1,setmealDtoPage);
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            Category category = categoryMapper.selectById(categoryId);
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }

    public SetmealDto getbyIdWithDish(long id){
        //查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //查询当前套餐对应的菜品信息
        lqw.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishMapper.selectList(lqw);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    public R<String> update( SetmealDto setmealDto){
        setmealMapper.updateById(setmealDto);
        //清除当前套餐的菜品
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lqw);
        //添加当前提交过来的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
       setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
       setmealDishService.saveBatch(setmealDishes);
        return R.success("修改套餐成功");

    }

    public R<String> delete(List<Long> ids){
        LambdaQueryWrapper<Setmeal> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Setmeal::getStatus,1);
        lqw1.in(Setmeal::getId,ids);
        int count = setmealMapper.selectCount(lqw1);
        if(count>0){
            throw new CustomException("此套餐正在售卖，不可删除！");
        }
        //删除套餐信息
        setmealMapper.deleteBatchIds(ids);
        //删除套餐关联的菜品信息
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(lqw);
        return R.success("删除套餐成功");

    }

    public R<String> updateStatus( Integer status,Long[] ids){
        LambdaUpdateWrapper<Setmeal> lqw = new LambdaUpdateWrapper<>();
        lqw.set(Setmeal::getStatus,status);
        lqw.in(Setmeal::getId,ids);
        setmealService.update(lqw);
        return R.success("套餐信息修改成功");
    }

    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealMapper.selectList(lqw);
        return R.success(list);

    }

}
